package com.tarry.thread.countdownlatch;

import java.util.concurrent.CountDownLatch;

public abstract class BaseHealthChecker implements Runnable {

    private CountDownLatch latch;
    private String serviceName;
    private Boolean serviceUp;

    public BaseHealthChecker(CountDownLatch latch, String serviceName) {
        this.latch = latch;
        this.serviceName = serviceName;
    }

    @Override
    public void run() {
        try {
            verifyService();
            this.serviceUp = true;
        }catch (Throwable throwable){
            throwable.printStackTrace(System.err);
            this.serviceUp = false;
        }finally {
            if (this.latch != null) {
                latch.countDown();
            }
        }
    }

    public abstract void verifyService();

    public String getServiceName() {
        return this.serviceName;
    }

    public boolean isServiceUp() {
        return this.serviceUp;
    }
}
